<?php
class Core_Data_Mappers_Payment extends Core_Data_Mappers_Abstract {
	protected $_modelclass = 'Core_Data_Models_Payment';
	protected $_map = array("Id"=>"id","Label"=>"label");
	protected $_businessclass = "Core_Business_Payment";
	
	/***
	 * find - search the first occurance of Category with identifier gives in argument
	 * @param $id int internal id for the category in database
	 * @return Core_Business_Category|null return an instance of Core_Business_Category
	 */
	public function find($id) {
		$rows = $this->getDbTable()->fetchAll($this->getDbTable()->select()->where('id = ?',$id),null,1,1);
		if (isset($rows)&& count($rows)==1) {
			$cat = $this->map($rows->current());
			return $cat;
		}
		else 
			return null;
	}
	/***
	 * Check if the object pass in argument exists.
	 * @param $object Core_Data_Mappers_Category 
	 * @return bool
	 */
	public function exists($object) {
		if ($object instanceof $this->_businessclass) {
			$obj = $this->find($object->Id);
			if (isset($obj))
				return true;
			else 
				return false;
		}
		else 
			throw new Exception("this instance wasn't an instance of " + $this->_businessclass);
	}
	
	public function add($object) {
		if($object instanceof $this->_businessclass) {
			if (!$this->exists($object)) {
				$id = $this->getDbTable()->insert($this->reversemap($object));
				$object->Id=$id;
			}
		}
	}
	
	public function edit($object) {
		if($object instanceof $this->_businessclass) {
			if ($this->exists($object)) {
				$data = $this->reversemap($object);
				unset($data['id']);	
				$this->getDbTable()->update($data, array('id = ?'=>$object->Id));
			}
		}
	}
	public function fetchAll($where=null,$count=10,$start=0) {
		$rows = $this->getDbTable()->fetchAll($where,null,$count,$start);
		if(isset($rows) && count($rows)>0) {
			$collection = array();
			foreach ($rows as $row)
				$collection[]=$this->map($row);
			
			return $collection;
		} 
		return array();
		
	}
	
}
?>